package com.zzyq.pc.mongodb.dao;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.aggregation.Accumulator;
import org.mongodb.morphia.aggregation.AggregationPipeline;
import org.mongodb.morphia.aggregation.Group;
import org.mongodb.morphia.query.Query;
import org.mongodb.morphia.query.Sort;

import com.mongodb.AggregationOptions;
import com.zzyq.pc.common.enums.Channel;
import com.zzyq.pc.common.enums.StarLevel;
import com.zzyq.pc.mongodb.model.MongodbDate;
import com.zzyq.pc.utils.LoggerUtils;
import com.zzyq.pc.utils.MongoHelper;
import com.zzyq.pc.utils.StringUtil;
import com.zzyq.pc.web.report.model.Report;

/**
 * 报告操作的Mongodb
 * 
 * @author shengchen
 *
 */
public class MongodbReport {

	MongoHelper mongodb = MongoHelper.getInstance();

	Datastore datastore = mongodb.getDatastore();

	SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

	/**
	 * 获取查询单位时间内目标方案信息的Query
	 * 
	 * @param entity
	 *            mongodbDate映射类
	 * @param IfmIds
	 *            方案ID集合
	 * @param startTime
	 *            大于等于此时间
	 * @param endTime
	 *            小于此时间
	 * @return Query
	 */
	public Query<?> getQueryByifmIdsAndTime(Class<?> entity, List<String> IfmIds, Long startTime, Long endTime) {
		Query<?> query = datastore.createQuery(entity);
		query.criteria("ifmId").in(IfmIds);
		query.criteria("sort_timestamp").greaterThanOrEq(startTime);
		query.criteria("sort_timestamp").lessThan(endTime);
		return query;
	}

	/**
	 * 比较两个值，计算环比
	 * 
	 * @param nowNum
	 *            当期数
	 * @param lastNum
	 *            上期数
	 * @return 环比数
	 */
	public Integer getMom(Integer nowNum, Integer lastNum) {
		if (null != nowNum && null != lastNum) {
			Double number = nowNum.doubleValue();
			Double lastNumber = lastNum.doubleValue();
			Integer total_compare = null;
			if (lastNum != 0) {
				total_compare = (int) (((number - lastNumber) / lastNumber) * 100);
				return total_compare;
			}
			LoggerUtils.debug(getClass(), "计算环比: lastNum = 0 所以返回环比增长 0");
		}
		LoggerUtils.debug(getClass(), "nowNum 或 lastNum 为null 所以返回环比增长 0");
		return 0;
	}

	/**
	 * 获取当前时间前一天的时间
	 * 
	 * @return "yyyy-HH-mm"
	 */
	public String getReprtName() {
		DateTime dt = new DateTime().withMillisOfDay(0).minusDays(1);
		return dt.toString("yyyy-MM-dd");
	}

	/**
	 * 下面的查询多次被调用 所以提取出来（查询敏感方案集合下时间段的所有信息）
	 *
	 * @param entity
	 * @param ifmIds
	 * @param startTime
	 * @param endTime
	 * @return List<MongodbDate>
	 */
	public List<MongodbDate> getWarningList(Class<?> entity, List<String> ifmIds, Long startTime, Long endTime) {
		Query<?> query = getQueryByifmIdsAndTime(entity, ifmIds, startTime, endTime);
		List<MongodbDate> list = new ArrayList<MongodbDate>();
		AggregationPipeline pipelineList = datastore.createAggregation(entity).match(query);
		pipelineList
				.group(Group.id(Group.grouping("groupId")), Group.grouping("groupId", Group.first("groupId")),
						Group.grouping("releaseTime", Group.first("releaseTime")),
						Group.grouping("sort_timestamp", Group.first("sort_timestamp")),
						Group.grouping("ifmId", Group.first("ifmId")),
						Group.grouping("articleId", Group.first("articleId")),
						Group.grouping("articleType", Group.first("articleType")),
						Group.grouping("title", Group.first("title")), Group.grouping("url", Group.first("url")),
						Group.grouping("websiteName", Group.first("websiteName")),
						Group.grouping("weiboSourceBlogger", Group.first("weiboSourceBlogger")),
						Group.grouping("weiboSourceBlogger", Group.first("weiboSourceBlogger")),
						Group.grouping("weiboSourceContent", Group.first("weiboSourceContent")),
						Group.grouping("weiboBlogger", Group.first("weiboBlogger")),
						Group.grouping("weiboContent", Group.first("weiboContent")),
						Group.grouping("author", Group.first("author")),
						Group.grouping("reprintNumb", new Accumulator("$sum", 1)))
				.sort(Sort.descending("reprintNumb")).limit(4000);
		Iterator<?> iteratorList = pipelineList.aggregate(entity,
				AggregationOptions.builder().allowDiskUse(true).build());
		while (iteratorList.hasNext()) {
			MongodbDate md = (MongodbDate) iteratorList.next();
			list.add(md);
		}
		return list;
	}


	/**
	 * 相比较warning方法，此方法直接接收集合数据，不用重复查询。对集合数据处理过滤 返回任意等级信息
	 * 
	 * @param List
	 * @param bigthenNum
	 * @param lessthenNum
	 * @return
	 */
	public List<MongodbDate> warningEllipsis(List<MongodbDate> List, Integer bigthenNum, Integer lessthenNum) {
		List<MongodbDate> modateList = new ArrayList<MongodbDate>();
		String WeibosourContent = null;
		String WeiboContent = null;
		for (int i = 0; i < List.size(); i++) {
			MongodbDate md = List.get(i);
			if (md.getReprintNumb() >= lessthenNum && md.getReprintNumb() < bigthenNum) {
				if (bigthenNum.equals(StarLevel.ThreeStar.getIndex())
						&& md.getArticleType().equals(Channel.NEWS.getName())) {
					continue;
				}
				if (StringUtils.isNotBlank(md.getTitle()) && !"None".equalsIgnoreCase(md.getTitle())) {
					md.setTitle(StringUtil.stringFilter(md.getTitle()));
				} else if (StringUtils.isNotBlank(md.getWeiboSourceContent())
						&& !"None".equalsIgnoreCase(md.getWeiboSourceContent())) {
					WeibosourContent = md.getWeiboSourceContent().replaceAll("<.*?em.*?>", "");
					WeibosourContent = StringUtil.stringFilter(WeibosourContent);
					md.setTitle(WeibosourContent);
				} else if (StringUtils.isNotBlank(md.getWeiboContent())
						&& !"None".equalsIgnoreCase(md.getWeiboContent())) {
					WeiboContent = md.getWeiboContent().replaceAll("<.*?em.*?>", "");
					WeiboContent = StringUtil.stringFilter(WeiboContent);
					md.setTitle(WeiboContent);
				}
				if (StringUtils.isNotBlank(md.getWebsiteName())) {
					md.setWebsiteName(md.getWebsiteName());
				} else {
					md.setWebsiteName(StringUtils.isNotBlank(md.getDomainName()) ? md.getDomainName() : "未知");
				}
				if (StringUtils.isNotBlank(md.getAuthor())) {
					md.setAuthor(md.getAuthor());
				} else {
					md.setAuthor(StringUtils.isNotBlank(md.getWeiboBlogger()) ? md.getWeiboBlogger() : "未知");
				}
				if (md.getTitle().length() > 50) {
					md.setTitle(md.getTitle().substring(0, 50));
				}
				if (md.getStarLevel() == null) {
					md.setStarLevel("");
				}
				if (md.getArticleType().equals(Channel.NEWS.getName())) {
					if (md.getReprintNumb() >= StarLevel.FiveStar.getIndex()) {
						md.setStarLevel("★★★★★");
					} else {
						md.setStarLevel("★★★★");
					}
				} else {
					if (md.getReprintNumb() >= StarLevel.FiveStar.getIndex()) {
						md.setStarLevel("★★★★★");
					} else if (md.getReprintNumb() > StarLevel.FourStar.getIndex()) {
						md.setStarLevel("★★★★");
					} else if (md.getReprintNumb() > StarLevel.ThreeStar.getIndex()) {
						md.setStarLevel("★★★");
					} else if (md.getReprintNumb() > StarLevel.TwoStar.getIndex()) {
						md.setStarLevel("★★");
					} else if (md.getReprintNumb() > StarLevel.OneStar.getIndex()) {
						md.setStarLevel("★");
					}
				}
				switch (md.getArticleType()) {
				case "BBS":
					md.setWebsiteName(Channel.BBS.getChineseName());
					break;
				case "WeiXin":
					md.setWebsiteName(Channel.WEIXIN.getChineseName());
					break;
				case "Micro":
					md.setWebsiteName(Channel.WEIBO.getChineseName());
					break;

				default:
					break;
				}

				modateList.add(md);
			} else if (bigthenNum.equals(StarLevel.limitlessStar.getIndex())) {// 预警舆情
				switch (md.getArticleType()) {
				case "BBS":
					md.setWebsiteName(Channel.BBS.getChineseName());
					break;
				case "WeiXin":
					md.setWebsiteName(Channel.WEIXIN.getChineseName());
					break;
				case "Micro":
					md.setWebsiteName(Channel.WEIBO.getChineseName());
					break;

				default:
					break;
				}
			} else if (bigthenNum.equals(StarLevel.FiveStar.getIndex())) {// 高危舆情
				// 新闻的需要被允许加入到高危舆情集合
				if (md.getArticleType().equals(Channel.NEWS.getName())
						&& (md.getReprintNumb() < StarLevel.ThreeStar.getIndex())) {
					// ----------重复上面代码-----------------------
					if (StringUtils.isNotBlank(md.getTitle()) && !"None".equalsIgnoreCase(md.getTitle())) {
						md.setTitle(StringUtil.stringFilter(md.getTitle()));
					} else if (StringUtils.isNotBlank(md.getWeiboSourceContent())
							&& !"None".equalsIgnoreCase(md.getWeiboSourceContent())) {
						WeibosourContent = md.getWeiboSourceContent().replaceAll("<.*?em.*?>", "");
						WeibosourContent = StringUtil.stringFilter(WeibosourContent);
						md.setTitle(WeibosourContent);
					} else if (StringUtils.isNotBlank(md.getWeiboContent())
							&& !"None".equalsIgnoreCase(md.getWeiboContent())) {
						WeiboContent = md.getWeiboContent().replaceAll("<.*?em.*?>", "");
						WeiboContent = StringUtil.stringFilter(WeiboContent);
						md.setTitle(WeiboContent);
					}
					if (StringUtils.isNotBlank(md.getWebsiteName())) {
						md.setWebsiteName(md.getWebsiteName());
					} else {
						md.setWebsiteName(StringUtils.isNotBlank(md.getDomainName()) ? md.getDomainName() : "");
					}
					if (StringUtils.isNotBlank(md.getAuthor())) {
						md.setAuthor(md.getAuthor());
					} else {
						md.setAuthor(StringUtils.isNotBlank(md.getWeiboBlogger()) ? md.getWeiboBlogger() : "未知");
					}
					if (md.getTitle().length() > 50) {
						md.setTitle(md.getTitle().substring(0, 50));
					}
					if (md.getStarLevel() == null) {
						md.setStarLevel("");
					}
					if (md.getArticleType().equals(Channel.NEWS.getName())) {
						if (md.getReprintNumb() >= StarLevel.FiveStar.getIndex()) {
							md.setStarLevel("★★★★★");
						} else {
							md.setStarLevel("★★★★");
						}
					} else {
						if (md.getReprintNumb() >= StarLevel.FiveStar.getIndex()) {
							md.setStarLevel("★★★★★");
						} else if (md.getReprintNumb() > StarLevel.FourStar.getIndex()) {
							md.setStarLevel("★★★★");
						} else if (md.getReprintNumb() > StarLevel.ThreeStar.getIndex()) {
							md.setStarLevel("★★★");
						} else if (md.getReprintNumb() > StarLevel.TwoStar.getIndex()) {
							md.setStarLevel("★★");
						} else if (md.getReprintNumb() > StarLevel.OneStar.getIndex()) {
							md.setStarLevel("★");
						}
					}
					switch (md.getArticleType()) {
					case "BBS":
						md.setWebsiteName(Channel.BBS.getChineseName());
						break;
					case "WeiXin":
						md.setWebsiteName(Channel.WEIXIN.getChineseName());
						break;
					case "Micro":
						md.setWebsiteName(Channel.WEIBO.getChineseName());
						break;

					default:
						break;
					}
					// ----------重复上面代码-----------------------
					modateList.add(md);
				}
			}
		}
		Collections.sort(modateList, new Comparator<MongodbDate>() {

			@Override
			public int compare(MongodbDate o1, MongodbDate o2) {
				if (o2.getStarLevel().compareTo(o1.getStarLevel()) == 0) {
					return o2.getReprintNumb().compareTo(o1.getReprintNumb());
				} else {
					return o2.getStarLevel().compareTo(o1.getStarLevel());
				}
			}

		});
		return modateList;
	}

	/**
	 * 返回任意星级段的舆情信息
	 * 
	 * @param entity
	 *            mongodbDate映射类
	 * @param ifmIds
	 *            方案ID集合
	 * @param bigthenNum
	 *            设定的 < 最大转载数
	 * @param lessthenNum
	 *            设定的 >= 最小转载数
	 * @param startTime
	 *            大于等于此时间
	 * @param endTime
	 *            小于此时间
	 * @return List<MongodbDate>集合 按星级排序最多4000条
	 */
	public List<MongodbDate> warning(Class<?> entity, List<String> ifmIds, Integer bigthenNum, Integer lessthenNum,
			Long startTime, Long endTime) {
		List<MongodbDate> modateList = new ArrayList<MongodbDate>();
		String WeibosourContent = null;
		String WeiboContent = null;
		Query<?> query = getQueryByifmIdsAndTime(entity, ifmIds, startTime, endTime);
		AggregationPipeline pipelineList = datastore.createAggregation(entity).match(query);
		pipelineList
				.group(Group.id(Group.grouping("groupId")), Group.grouping("groupId", Group.first("groupId")),
						Group.grouping("releaseTime", Group.first("releaseTime")),
						Group.grouping("sort_timestamp", Group.first("sort_timestamp")),
						Group.grouping("ifmId", Group.first("ifmId")),
						Group.grouping("articleId", Group.first("articleId")),
						Group.grouping("articleType", Group.first("articleType")),
						Group.grouping("title", Group.first("title")), Group.grouping("url", Group.first("url")),
						Group.grouping("websiteName", Group.first("websiteName")),
						Group.grouping("weiboSourceBlogger", Group.first("weiboSourceBlogger")),
						Group.grouping("weiboSourceBlogger", Group.first("weiboSourceBlogger")),
						Group.grouping("weiboSourceContent", Group.first("weiboSourceContent")),
						Group.grouping("weiboBlogger", Group.first("weiboBlogger")),
						Group.grouping("weiboContent", Group.first("weiboContent")),
						Group.grouping("author", Group.first("author")),
						Group.grouping("reprintNumb", new Accumulator("$sum", 1)))
				.sort(Sort.descending("reprintNumb")).limit(4000);
		Iterator<?> iteratorList = pipelineList.aggregate(entity,
				AggregationOptions.builder().allowDiskUse(true).build());
		while (iteratorList.hasNext()) {
			MongodbDate md = (MongodbDate) iteratorList.next();
			if (md.getReprintNumb() >= lessthenNum && md.getReprintNumb() < bigthenNum) {
				if (bigthenNum.equals(StarLevel.ThreeStar.getIndex())
						&& md.getArticleType().equals(Channel.NEWS.getName())) {
					continue;
				}
				if (StringUtils.isNotBlank(md.getTitle()) && !"None".equalsIgnoreCase(md.getTitle())) {
					md.setTitle(StringUtil.stringFilter(md.getTitle()));
				} else if (StringUtils.isNotBlank(md.getWeiboSourceContent())
						&& !"None".equalsIgnoreCase(md.getWeiboSourceContent())) {
					WeibosourContent = md.getWeiboSourceContent().replaceAll("<.*?em.*?>", "");
					WeibosourContent = StringUtil.stringFilter(WeibosourContent);
					md.setTitle(WeibosourContent);
				} else if (StringUtils.isNotBlank(md.getWeiboContent())
						&& !"None".equalsIgnoreCase(md.getWeiboContent())) {
					WeiboContent = md.getWeiboContent().replaceAll("<.*?em.*?>", "");
					WeiboContent = StringUtil.stringFilter(WeiboContent);
					md.setTitle(WeiboContent);
				}
				if (StringUtils.isNotBlank(md.getWebsiteName())) {
					md.setWebsiteName(md.getWebsiteName());
				} else {
					md.setWebsiteName(StringUtils.isNotBlank(md.getDomainName()) ? md.getDomainName() : "未知");
				}
				if (StringUtils.isNotBlank(md.getAuthor())) {
					md.setAuthor(md.getAuthor());
				} else {
					md.setAuthor(StringUtils.isNotBlank(md.getWeiboBlogger()) ? md.getWeiboBlogger() : "未知");
				}
				if (md.getTitle().length() > 50) {
					md.setTitle(md.getTitle().substring(0, 50));
				}
				if (md.getStarLevel() == null) {
					md.setStarLevel("");
				}
				if (md.getArticleType().equals(Channel.NEWS.getName())) {
					if (md.getReprintNumb() >= StarLevel.FiveStar.getIndex()) {
						md.setStarLevel("★★★★★");
					} else {
						md.setStarLevel("★★★★");
					}
				} else {
					if (md.getReprintNumb() >= StarLevel.FiveStar.getIndex()) {
						md.setStarLevel("★★★★★");
					} else if (md.getReprintNumb() > StarLevel.FourStar.getIndex()) {
						md.setStarLevel("★★★★");
					} else if (md.getReprintNumb() > StarLevel.ThreeStar.getIndex()) {
						md.setStarLevel("★★★");
					} else if (md.getReprintNumb() > StarLevel.TwoStar.getIndex()) {
						md.setStarLevel("★★");
					} else if (md.getReprintNumb() > StarLevel.OneStar.getIndex()) {
						md.setStarLevel("★");
					}
				}
				switch (md.getArticleType()) {
				case "BBS":
					md.setWebsiteName(Channel.BBS.getChineseName());
					break;
				case "WeiXin":
					md.setWebsiteName(Channel.WEIXIN.getChineseName());
					break;
				case "Micro":
					md.setWebsiteName(Channel.WEIBO.getChineseName());
					break;

				default:
					break;
				}
				modateList.add(md);
			} else if (bigthenNum.equals(StarLevel.limitlessStar.getIndex())) {// 预警舆情
				switch (md.getArticleType()) {
				case "BBS":
					md.setWebsiteName(Channel.BBS.getChineseName());
					break;
				case "WeiXin":
					md.setWebsiteName(Channel.WEIXIN.getChineseName());
					break;
				case "Micro":
					md.setWebsiteName(Channel.WEIBO.getChineseName());
					break;

				default:
					break;
				}
			} else if (bigthenNum.equals(StarLevel.FiveStar.getIndex())) {// 高危舆情
				// 新闻的需要被允许加入到高危舆情集合
				if (md.getArticleType().equals(Channel.NEWS.getName())
						&& (md.getReprintNumb() < StarLevel.ThreeStar.getIndex())) {
					// ----------重复上面代码-----------------------
					if (StringUtils.isNotBlank(md.getTitle()) && !"None".equalsIgnoreCase(md.getTitle())) {
						md.setTitle(StringUtil.stringFilter(md.getTitle()));
					} else if (StringUtils.isNotBlank(md.getWeiboSourceContent())
							&& !"None".equalsIgnoreCase(md.getWeiboSourceContent())) {
						WeibosourContent = md.getWeiboSourceContent().replaceAll("<.*?em.*?>", "");
						WeibosourContent = StringUtil.stringFilter(WeibosourContent);
						md.setTitle(WeibosourContent);
					} else if (StringUtils.isNotBlank(md.getWeiboContent())
							&& !"None".equalsIgnoreCase(md.getWeiboContent())) {
						WeiboContent = md.getWeiboContent().replaceAll("<.*?em.*?>", "");
						WeiboContent = StringUtil.stringFilter(WeiboContent);
						md.setTitle(WeiboContent);
					}
					if (StringUtils.isNotBlank(md.getWebsiteName())) {
						md.setWebsiteName(md.getWebsiteName());
					} else {
						md.setWebsiteName(StringUtils.isNotBlank(md.getDomainName()) ? md.getDomainName() : "");
					}
					if (StringUtils.isNotBlank(md.getAuthor())) {
						md.setAuthor(md.getAuthor());
					} else {
						md.setAuthor(StringUtils.isNotBlank(md.getWeiboBlogger()) ? md.getWeiboBlogger() : "未知");
					}
					if (md.getTitle().length() > 50) {
						md.setTitle(md.getTitle().substring(0, 50));
					}
					if (md.getStarLevel() == null) {
						md.setStarLevel("");
					}
					if (md.getArticleType().equals(Channel.NEWS.getName())) {
						if (md.getReprintNumb() >= StarLevel.FiveStar.getIndex()) {
							md.setStarLevel("★★★★★");
						} else {
							md.setStarLevel("★★★★");
						}
					} else {
						if (md.getReprintNumb() >= StarLevel.FiveStar.getIndex()) {
							md.setStarLevel("★★★★★");
						} else if (md.getReprintNumb() > StarLevel.FourStar.getIndex()) {
							md.setStarLevel("★★★★");
						} else if (md.getReprintNumb() > StarLevel.ThreeStar.getIndex()) {
							md.setStarLevel("★★★");
						} else if (md.getReprintNumb() > StarLevel.TwoStar.getIndex()) {
							md.setStarLevel("★★");
						} else if (md.getReprintNumb() > StarLevel.OneStar.getIndex()) {
							md.setStarLevel("★");
						}
					}
					switch (md.getArticleType()) {
					case "BBS":
						md.setWebsiteName(Channel.BBS.getChineseName());
						break;
					case "WeiXin":
						md.setWebsiteName(Channel.WEIXIN.getChineseName());
						break;
					case "Micro":
						md.setWebsiteName(Channel.WEIBO.getChineseName());
						break;

					default:
						break;
					}
					// ----------重复上面代码-----------------------
					modateList.add(md);
				}
			}
		}
		Collections.sort(modateList, new Comparator<MongodbDate>() {

			@Override
			public int compare(MongodbDate o1, MongodbDate o2) {
				if (o2.getStarLevel().compareTo(o1.getStarLevel()) == 0) {
					return o2.getReprintNumb().compareTo(o1.getReprintNumb());
				} else {
					return o2.getStarLevel().compareTo(o1.getStarLevel());
				}
			}

		});
		return modateList;
	}

	/**
	 * 计算环比参数返回报告对象
	 * 
	 * @param entity
	 *            mongodbDate映射类
	 * @param allIfmIds
	 *            方案ID集合
	 * @param negativeIfmIds
	 *            敏感的方案ID集合
	 * @param lastNumber
	 *            上期总数
	 * @param lastPositiveNumber
	 *            上期正面数
	 * @param lastNegativeNumber
	 *            上期负面数
	 * @param companyId
	 *            报告所属公司ID
	 * @return Report报告对象
	 */
	public Report getTodayReport(Class<?> entity, List<String> allIfmIds, List<String> negativeIfmIds,
			Integer lastNumber, Integer lastPositiveNumber, Integer lastNegativeNumber, Integer companyId) {
		Report report = new Report();
		DateTime todayerday = new DateTime().withMillisOfDay(0);// 当前时间零点
		DateTime yesterday = todayerday.minusDays(1);// 昨日零点
		Integer number = null;// 本期总数
		Integer positiveNumber = null;// 本期正面数
		Integer negativeNumber = null;// 本期负面数
		Integer total_compare = null;// 总数环比
		Integer positive_compare = null;// 正面环比
		Integer negative_compare = null;// 负面环比
		Query<?> query = getQueryByifmIdsAndTime(entity, allIfmIds, yesterday.getMillis(), todayerday.getMillis());
		number = (int) query.count();
		Query<?> negativeQuery = getQueryByifmIdsAndTime(entity, negativeIfmIds, yesterday.getMillis(),
				todayerday.getMillis());
		negativeNumber = (int) negativeQuery.count();
		positiveNumber = number - negativeNumber;
		total_compare = getMom(number, lastNumber);
		positive_compare = getMom(positiveNumber, lastPositiveNumber);
		negative_compare = getMom(negativeNumber, lastNegativeNumber);
		report.setReportName(getReprtName());
		report.setCreateTime(new Date());
		report.setNegativeCompare(negative_compare);
		report.setNegativeNum(negativeNumber);
		report.setPositiveCompare(positive_compare);
		report.setPositiveNum(positiveNumber);
		report.setTotal(number);
		report.setTotalCompare(total_compare);
		report.setCompanyId(companyId);
		report.setReportTime(yesterday.toDate());
		return report;
	}

	/**
	 * 重载上面的方法 直接传入开始结束时间
	 * 
	 * @param entity
	 * @param allIfmIds
	 * @param negativeIfmIds
	 * @param lastNumber
	 * @param lastPositiveNumber
	 * @param lastNegativeNumber
	 * @param companyId
	 * @param todayerday
	 * @param yesterday
	 * @return
	 */
	public Report getTodayReport2(Class<?> entity, List<String> allIfmIds, List<String> negativeIfmIds,
			Integer lastNumber, Integer lastPositiveNumber, Integer lastNegativeNumber, Integer companyId,
			DateTime todayerday, DateTime yesterday) {
		Report report = new Report();
		Integer number = null;// 本期总数
		Integer positiveNumber = null;// 本期正面数
		Integer negativeNumber = null;// 本期负面数
		Integer total_compare = null;// 总数环比
		Integer positive_compare = null;// 正面环比
		Integer negative_compare = null;// 负面环比
		Query<?> query = getQueryByifmIdsAndTime(entity, allIfmIds, yesterday.getMillis(), todayerday.getMillis());
		number = (int) query.count();
		Query<?> negativeQuery = getQueryByifmIdsAndTime(entity, negativeIfmIds, yesterday.getMillis(),
				todayerday.getMillis());
		negativeNumber = (int) negativeQuery.count();
		positiveNumber = number - negativeNumber;
		total_compare = getMom(number, lastNumber);
		positive_compare = getMom(positiveNumber, lastPositiveNumber);
		negative_compare = getMom(negativeNumber, lastNegativeNumber);
		report.setReportName(yesterday.toString("yyyy-MM-dd"));
		report.setCreateTime(todayerday.plusHours(8).toDate());
		report.setNegativeCompare(negative_compare);
		report.setNegativeNum(negativeNumber);
		report.setPositiveCompare(positive_compare);
		report.setPositiveNum(positiveNumber);
		report.setTotal(number);
		report.setTotalCompare(total_compare);
		report.setCompanyId(companyId);
		report.setReportTime(yesterday.toDate());
		return report;
	}

	public Report getlastdayReport(Class<?> entity, List<String> allIfmIds, List<String> negativeIfmIds,
			Integer lastNumber, Integer lastPositiveNumber, Integer lastNegativeNumber, Integer companyId,
			Long startTime, Long endTime) {
		Report report = new Report();
		Integer number = null;// 本期总数
		Integer positiveNumber = null;// 本期正面数
		Integer negativeNumber = null;// 本期负面数
		Integer total_compare = null;// 总数环比
		Integer positive_compare = null;// 正面环比
		Integer negative_compare = null;// 负面环比
		Query<?> query = getQueryByifmIdsAndTime(entity, allIfmIds, startTime, endTime);
		number = (int) query.count();
		Query<?> negativeQuery = getQueryByifmIdsAndTime(entity, negativeIfmIds, startTime, endTime);
		negativeNumber = (int) negativeQuery.count();
		positiveNumber = number - negativeNumber;
		total_compare = getMom(number, lastNumber);
		positive_compare = getMom(positiveNumber, lastPositiveNumber);
		negative_compare = getMom(negativeNumber, lastNegativeNumber);
		report.setReportName(new DateTime(startTime).toString("yyyy-MM-dd"));
		report.setCreateTime(new Date());
		report.setNegativeCompare(negative_compare);
		report.setNegativeNum(negativeNumber);
		report.setPositiveCompare(positive_compare);
		report.setPositiveNum(positiveNumber);
		report.setTotal(number);
		report.setTotalCompare(total_compare);
		report.setCompanyId(companyId);
		report.setReportTime(new Date(startTime));
		return report;
	}

	/**
	 * 仅查询报告三个数
	 * 
	 * @param entity
	 *            mongodbDate映射类
	 * @param allIfmIds
	 *            方案ID集合
	 * @param negativeIfmIds
	 *            敏感的方案ID集合
	 * @param startTime
	 *            查询开始时间
	 * @param endTime
	 *            查询结束时间
	 * @return Report对象
	 */
	public Report getReportNum(Class<?> entity, List<String> allIfmIds, List<String> negativeIfmIds, Long startTime,
			Long endTime) {
		Report report = new Report();
		Query<?> query = getQueryByifmIdsAndTime(entity, allIfmIds, startTime, endTime);
		report.setTotal((int) query.count());
		Query<?> negativeQuery = getQueryByifmIdsAndTime(entity, negativeIfmIds, startTime, endTime);
		report.setNegativeNum((int) negativeQuery.count());
		report.setPositiveNum(report.getTotal() - report.getNegativeNum());
		return report;
	}

	public static void main(String[] args) {
		// MongodbReport md = new MongodbReport();
		// System.out.println(md.getReprtName());
		// List<String> allIfmIds = new ArrayList<>();
		// List<String> negativeIfmIds = new ArrayList<>();
		// allIfmIds.add("234");
		// allIfmIds.add("358");
		// negativeIfmIds.add("358");
		// Report r = md.getTodayReport(MongodbDate.class, allIfmIds,
		// negativeIfmIds, 30, 28, 2, 1);
		// System.out.println(r.toString());
		// for (MongodbDate mongodbDate : list) {
		// System.out.println(mongodbDate.getIfmId() + " Title:" +
		// mongodbDate.getTitle() + " WebsiteName:"
		// + mongodbDate.getWebsiteName() + " Author:" + mongodbDate.getAuthor()
		// + " ReleaseTime:" + mongodbDate.getReleaseTime() + " ReprintNumb:" +
		// mongodbDate.getReprintNumb());
		// }
		DateTime dt = new DateTime().withMillisOfDay(0).minusDays(1);
		System.out.println(dt.toString("yyyy-MM-dd"));
	}

}
